package com.ericpol.selenium;

import static org.testng.Assert.assertTrue;

import java.util.NoSuchElementException;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeMethod;

public class SeleniumBase {

	protected WebDriver driver;

	protected void openUrl(String baseUrl) {
		driver.get(baseUrl);
	}

	protected void findAndClick(By by) {
		driver.findElement(by).click();
	}

	protected void insertText(By by, String text) {
		WebElement element = driver.findElement(by);
		element.clear();
		element.sendKeys(text);
	}

	protected void assertTextPresent(String expected) {
		assertTrue(driver.getPageSource().contains(expected));
	}

	protected boolean isPresentText(By by) {
		try {
			driver.findElement(by);
			return true;
		} catch (NoSuchElementException e) {
			return false;
		}
	}

	@BeforeMethod
	public void setUp() throws Exception {
		driver = new HtmlUnitDriver();
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
	}

	@AfterClass
	public void tearDown() throws Exception {
		driver.quit();
	}

}
